%MACRO SEBORG_REDUCIDO (print=0, COMPONENTES1=, COMPONENTES2=, ID1=ESTACION, ID2=ESTACION, 
					   IDENT1='EST', IDENT2='EST', VAR=, ALFA=0.5,
					MEDIAS1= , VARIANZAS1=, NVALIDOS1=, AUTOVALOR1=AUTOVALOR1,
					MEDIAS2=, VARIANZAS2=, NVALIDOS2=,  AUTOVALOR2=AUTOVALOR2,
					SIMILCOMP=similcomp,  SIMILMAH=similmah, DISTMAH=distmah,
					SIMILGLOBAL=similglobal, NCLUSTER=, landas=1,
					metodosinghal=0);


%DISTsinghal (data1=&COMPONENTES1, DATA2=&COMPONENTES2, id1=&ID1, ID2=&ID2,
				IDENT1=&IDENT1 , IDENT2=&IDENT2, similitud=&SIMILCOMP, print=&print,
				  autovalor1=&autovalor1, autovalor2=&autovalor2, var=&var); 

 
%distmahala (DATAMEDIAS1=&MEDIAS1, DATACOVAR1=&VARIANZAS1, DATAN1=&NVALIDOS1,
			DATAMEDIAS2=&MEDIAS2, DATACOVAR2=&VARIANZAS2, DATAN2=&NVALIDOS2,
			ID1=&ID1 , ID2=&ID2, IDENT1=&IDENT1, IDENT2=&ident2, VAR=&VAR, 
					MAHALANOB=&DISTMAH	, SIMILMAH=&SIMILMAH, print=&print) ;

%if &metodosinghal=1 %then %do; 
	data similb; set sim_javier;run;
	%end; 
%else %do; 
	data similb; set &similmah;run;
	%end;


  /**COMBINAR LAS SIMILITUDES ****/
%if &landas=1 %then %do; 

PROC IML; 
      USE &SIMILMAH; 
	  READ ALL var {filas} into filas; 
	  READ ALL VAR _NUM_  INTO MAH; 
  
	  USE SIMIL_conlanda; 
	  READ ALL VAR _NUM_ INTO COMP; 

	  COM=&ALFA*(MAH)+ (1-&ALFA)*COMP; 
       
	FILAS=T(filas);
	CLUU=J(1,&NCLUSTER,'CLUSTER'); 
	CLUU2=1:&NCLUSTER; 


	IF &NCLUSTER<10 THEN CLUU2B=CHAR(CLUU2,1); 
	ELSE CLUU2B=CHAR(CLUU2,2); 
	COLUMNAS=CONCAT(CLUU,CLUU2B);
	COLUMNAS=COMPRESS(COLUMNAS);
	

create &SIMILGLOBAL from com [ROWNAME=FILAS COLNAME=COLUMNAS];
append from com [ROWNAME=FILAS]; 
close  &SIMILGLOBAL;
quit; 
TITLE 'SIMILITUD GLOBAL GLOBAL';
/*proc print data=&similglobal;run;**/
TITLE; 

%end;



%else %do;
PROC IML; 
      USE &SIMILMAH; 
	  READ ALL var {filas} into filas; 
	  READ ALL VAR _NUM_  INTO MAH; 
  
	  USE &SIMILCOMP; 
	  READ ALL VAR _NUM_ INTO COMP; 

	  COM=&ALFA*(MAH)+ (1-&ALFA)*COMP; 
       
	FILAS=T(filas);
	CLUU=J(1,&NCLUSTER,'CLUSTER'); 
	CLUU2=1:&NCLUSTER; 


	IF &NCLUSTER<10 THEN CLUU2B=CHAR(CLUU2,1); 
	ELSE CLUU2B=CHAR(CLUU2,2); 
	COLUMNAS=CONCAT(CLUU,CLUU2B);
	COLUMNAS=COMPRESS(COLUMNAS);
	

create &SIMILGLOBAL from com [ROWNAME=FILAS COLNAME=COLUMNAS];
append from com [ROWNAME=FILAS]; 
close  &SIMILGLOBAL;
quit; 
TITLE 'SIMILITUD GLOBAL GLOBAL';
/*proc print data=&similglobal;run;**/
TITLE; 
%end;

%MEND SEBORG_REDUCIDO;
